#ifndef __TYPEDEF_H__
#define __TYPEDEF_H__

#include "PlatformTypes.h"
#include "StandardTypes.h"



/*
********************************************************************************
*    Global Types
********************************************************************************
*/
#define be16_to_cpu(x) \
         ((((x) >> 8) & 0x00FF) | \
         (((x) << 8) & 0xFF00))
#define be32_to_cpu(x) \
        ((((x) >> 24) & 0x000000FF) | \
         (((x) >> 8)  & 0x0000FF00) | \
         (((x) << 8)  & 0x00FF0000) | \
         (((x) << 24) & 0xFF000000))
#define be64_to_cpu(x) \
        ((((x) >> 56) & 0x00000000000000FF) | \
         (((x) >> 40) & 0x000000000000FF00) | \
         (((x) >> 24) & 0x00000000FF000000) | \
         (((x) >> 8)  & 0x00FF000000000000) | \
         (((x) << 8)  & 0xFF00000000000000) | \
         (((x) << 24) & 0x0000FF0000000000) | \
         (((x) << 40) & 0x00FF000000000000) | \
         (((x) << 56) & 0xFF00000000000000))

#define cpu_to_be16(x) \
         ((((x) >> 8) & 0x00FF) | \
         (((x) << 8) & 0xFF00))
#define cpu_to_be32(x) \
        ((((x) >> 24) & 0x000000FF) | \
         (((x) >> 8)  & 0x0000FF00) | \
         (((x) << 8)  & 0x00FF0000) | \
         (((x) << 24) & 0xFF000000))
#define cpu_to_be64(x) \
        ((((x) >> 56) & 0x00000000000000FF) | \
         (((x) >> 40) & 0x000000000000FF00) | \
         (((x) >> 24) & 0x0000000000FF0000) | \
         (((x) >> 8)  & 0x00000000FF000000) | \
         (((x) << 8)  & 0x000000FF00000000) | \
         (((x) << 24) & 0x0000FF0000000000) | \
         (((x) << 40) & 0x00FF000000000000) | \
         (((x) << 56) & 0xFF00000000000000))


/*
********************************************************************************
*    Global Function
********************************************************************************
*/
static inline uint16 __get_unaligned_le16(const uint8 *p)
{
	return p[0] | p[1] << 8;
}

static inline uint32 __get_unaligned_le32(const uint8 *p)
{
	return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
}

static inline uint64 __get_unaligned_le64(const uint8 *p)
{
	return (uint64)__get_unaligned_le32(p + 4) << 32 |
		__get_unaligned_le32(p);
}

static inline void __put_unaligned_le16(uint16 val, uint8 *p)
{
	*p++ = val;
	*p++ = val >> 8;
}

static inline void __put_unaligned_le32(uint32 val, uint8 *p)
{
	__put_unaligned_le16(val >> 16, p + 2);
	__put_unaligned_le16(val, p);
}

static inline void __put_unaligned_le64(uint64 val, uint8 *p)
{
	__put_unaligned_le32(val >> 32, p + 4);
	__put_unaligned_le32(val, p);
}

static inline uint16 get_unaligned_le16(const void *p)
{
	return __get_unaligned_le16((const uint8 *)p);
}

static inline uint32 get_unaligned_le32(const void *p)
{
	return __get_unaligned_le32((const uint8 *)p);
}

static inline uint64 get_unaligned_le64(const void *p)
{
	return __get_unaligned_le64((const uint8 *)p);
}

static inline void put_unaligned_le16(uint16 val, void *p)
{
	__put_unaligned_le16(val, p);
}

static inline void put_unaligned_le32(uint32 val, void *p)
{
	__put_unaligned_le32(val, p);
}

static inline void put_unaligned_le64(uint64 val, void *p)
{
	__put_unaligned_le64(val, p);
}

static inline uint16 __get_unaligned_be16(const uint8 *p)
{
	return p[0] << 8 | p[1];
}

static inline uint32 __get_unaligned_be32(const uint8 *p)
{
	return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
}

static inline uint64 __get_unaligned_be64(const uint8 *p)
{
	return (uint64)__get_unaligned_be32(p) << 32 |
		__get_unaligned_be32(p + 4);
}

static inline void __put_unaligned_be16(uint16 val, uint8 *p)
{
	*p++ = val >> 8;
	*p++ = val;
}

static inline void __put_unaligned_be32(uint32 val, uint8 *p)
{
	__put_unaligned_be16(val >> 16, p);
	__put_unaligned_be16(val, p + 2);
}

static inline void __put_unaligned_be64(uint64 val, uint8 *p)
{
	__put_unaligned_be32(val >> 32, p);
	__put_unaligned_be32(val, p + 4);
}

static inline uint16 get_unaligned_be16(const void *p)
{
	return __get_unaligned_be16((const uint8 *)p);
}

static inline uint32 get_unaligned_be32(const void *p)
{
	return __get_unaligned_be32((const uint8 *)p);
}

static inline uint64 get_unaligned_be64(const void *p)
{
	return __get_unaligned_be64((const uint8 *)p);
}

static inline void put_unaligned_be16(uint16 val, void *p)
{
	__put_unaligned_be16(val, p);
}

static inline void put_unaligned_be32(uint32 val, void *p)
{
	__put_unaligned_be32(val, p);
}

static inline void put_unaligned_be64(uint64 val, void *p)
{
	__put_unaligned_be64(val, p);
}

#endif
